package com.ssy.lingxi.logistics.serviceimpl;

import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.logistics.api.dto.request.CommonIdRequest;
import com.ssy.lingxi.logistics.api.dto.response.LogisticsOrderFeignResponse;
import com.ssy.lingxi.logistics.entity.LogisticsOrder;
import com.ssy.lingxi.logistics.repository.LogisticsOrderDetailRepository;
import com.ssy.lingxi.logistics.repository.LogisticsOrderRepository;
import com.ssy.lingxi.logistics.service.ILogisticsOrderFeignService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 物流单feign实现服务类
 * @author yzc
 * @version 2.0.0
 * @date 2021/1/26
 */
@Slf4j
@Service
public class LogisticsOrderFeignServiceImpl implements ILogisticsOrderFeignService {

    @Resource
    private LogisticsOrderRepository logisticsOrderRepository;
    @Resource
    private LogisticsOrderDetailRepository logisticsOrderDetailRepository;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Wrapper<Void> deleteLogisticsOrder(CommonIdRequest request) {
        LogisticsOrder logisticsOrder = logisticsOrderRepository.findById(request.getId()).orElse(null);
        if (Objects.isNull(logisticsOrder)) {
            return Wrapper.fail(ResponseCode.LS_RECORDS_DON_T_EXIST);
        }

        logisticsOrderRepository.deleteById(logisticsOrder.getId());

        // 删除关联的订单明细
        logisticsOrderDetailRepository.deleteByOrderId(logisticsOrder.getId());
        return Wrapper.success();

    }

    @Override
    public Wrapper<LogisticsOrderFeignResponse> getLogisticsOrder(Long id) {
        LogisticsOrder logisticsOrder = logisticsOrderRepository.findById(id).orElse(null);
        if (Objects.isNull(logisticsOrder)) {
            return Wrapper.fail(ResponseCode.LS_RECORDS_DON_T_EXIST);
        }

        LogisticsOrderFeignResponse logisticsOrderFeignResponse = new LogisticsOrderFeignResponse();
        logisticsOrderFeignResponse.setId(logisticsOrder.getId());
        logisticsOrderFeignResponse.setLogisticsOrderNo(logisticsOrder.getLogisticsOrderNo());
        logisticsOrderFeignResponse.setDigest(logisticsOrder.getDigest());
        logisticsOrderFeignResponse.setCompanyId(logisticsOrder.getCompanyId());
        logisticsOrderFeignResponse.setCompanyName(logisticsOrder.getCompanyName());
        logisticsOrderFeignResponse.setShipperMemberId(logisticsOrder.getMemberId());
        logisticsOrderFeignResponse.setShipperRoleId(logisticsOrder.getRoleId());
        logisticsOrderFeignResponse.setShipperMemberName(logisticsOrder.getMemberName());
        logisticsOrderFeignResponse.setShipperName(logisticsOrder.getShipperName());
        logisticsOrderFeignResponse.setShipperPhone(logisticsOrder.getShipperPhone());
        logisticsOrderFeignResponse.setShipperAddressId(logisticsOrder.getShipperAddressId());
        logisticsOrderFeignResponse.setShipperFullAddress(logisticsOrder.getShipperFullAddress());
        logisticsOrderFeignResponse.setReceiverMemberId(logisticsOrder.getReceiverMemberId());
        logisticsOrderFeignResponse.setReceiverRoleId(logisticsOrder.getReceiverRoleId());
        logisticsOrderFeignResponse.setReceiverMemberName(logisticsOrder.getReceiverMemberName());
        logisticsOrderFeignResponse.setReceiverName(logisticsOrder.getReceiverName());
        logisticsOrderFeignResponse.setReceiverPhone(logisticsOrder.getReceiverPhone());
        logisticsOrderFeignResponse.setReceiverAddressId(logisticsOrder.getReceiverAddressId());
        logisticsOrderFeignResponse.setReceiverFullAddress(logisticsOrder.getReceiverFullAddress());

        return Wrapper.success(logisticsOrderFeignResponse);
    }

    @Override
    public Wrapper<List<LogisticsOrderFeignResponse>> listLogisticsOrder(List<Long> ids) {
        List<LogisticsOrder> logisticsOrderList = logisticsOrderRepository.findAllById(ids);
        if (CollectionUtils.isEmpty(logisticsOrderList)) {
            return Wrapper.fail(ResponseCode.LS_RECORDS_DON_T_EXIST);
        }

        List<LogisticsOrderFeignResponse> logisticsOrderFeignResponseList = logisticsOrderList.stream().map(logisticsOrder -> {
            LogisticsOrderFeignResponse logisticsOrderFeignResponse = new LogisticsOrderFeignResponse();
            logisticsOrderFeignResponse.setId(logisticsOrder.getId());
            logisticsOrderFeignResponse.setLogisticsOrderNo(logisticsOrder.getLogisticsOrderNo());
            logisticsOrderFeignResponse.setDigest(logisticsOrder.getDigest());
            logisticsOrderFeignResponse.setCompanyId(logisticsOrder.getCompanyId());
            logisticsOrderFeignResponse.setCompanyName(logisticsOrder.getCompanyName());
            logisticsOrderFeignResponse.setShipperMemberId(logisticsOrder.getMemberId());
            logisticsOrderFeignResponse.setShipperRoleId(logisticsOrder.getRoleId());
            logisticsOrderFeignResponse.setShipperMemberName(logisticsOrder.getMemberName());
            logisticsOrderFeignResponse.setShipperName(logisticsOrder.getShipperName());
            logisticsOrderFeignResponse.setShipperPhone(logisticsOrder.getShipperPhone());
            logisticsOrderFeignResponse.setShipperAddressId(logisticsOrder.getShipperAddressId());
            logisticsOrderFeignResponse.setShipperFullAddress(logisticsOrder.getShipperFullAddress());
            logisticsOrderFeignResponse.setReceiverMemberId(logisticsOrder.getReceiverMemberId());
            logisticsOrderFeignResponse.setReceiverRoleId(logisticsOrder.getReceiverRoleId());
            logisticsOrderFeignResponse.setReceiverMemberName(logisticsOrder.getReceiverMemberName());
            logisticsOrderFeignResponse.setReceiverName(logisticsOrder.getReceiverName());
            logisticsOrderFeignResponse.setReceiverPhone(logisticsOrder.getReceiverPhone());
            logisticsOrderFeignResponse.setReceiverAddressId(logisticsOrder.getReceiverAddressId());
            logisticsOrderFeignResponse.setReceiverFullAddress(logisticsOrder.getReceiverFullAddress());
            return logisticsOrderFeignResponse;
        }).collect(Collectors.toList());


        return Wrapper.success(logisticsOrderFeignResponseList);
    }
}